package com.example.utility;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Ffmpeggjl {
    public static void toCompressFile(String convertFile, String targetFile){
        try{
            Runtime runtime = Runtime.getRuntime();
            /**将视频压缩为 每秒15帧 平均码率600k 画面的宽与高 为1280*720*/
            //ffmpeg -i Desktop/1.mov -b:v 1.5M  Desktop/1.mp4
            String cutCmd="ffmpeg -i " + convertFile + " -r 15 -b:v 600k  -s 1280x720 "+ targetFile;
            System.out.println("cutCmd:"+cutCmd);
            runCmd(cutCmd);
            System.out.println("文件："+convertFile+" 视屏压缩完成");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("压缩文件出现异常："+e.getMessage());
        }
    }

    public static void runCmd(String command) {
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(command);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line;
            System.out.println("开始执行");
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
            System.out.println("执行结束");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace();
        }
    }
}
